home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_07 / allison / destroy1.cpp < prev    next >
C/C++ Source or Header  |  1994-05-02  |  589b  |  47 lines

  1. LISTING 1 - Illustrates objects left undestroyed by longjmp
  2.  
  3. // destroy1.cpp
  4. #include <iostream.h>
  5. #include <setjmp.h>
  6.  
  7. class Foo
  8. {
  9. public:
  10.     Foo() {cout << "Foo constructor" << endl;}
  11.     ~Foo() {cout << "Foo destructor" << endl;}
  12. };
  13.  
  14. void f();
  15. void g();
  16.  
  17. jmp_buf env;
  18.  
  19. main()
  20. {
  21.     if (setjmp(env) == 0)
  22.         f();
  23.     else
  24.         cout << "Returned via longjmp" << endl;
  25.     return 0;
  26. }
  27.  
  28. void f()
  29. {
  30.     Foo x;
  31.  
  32.     g();
  33. }
  34.  
  35. void g()
  36. {
  37.     Foo x;
  38.  
  39.     longjmp(env,1);
  40. }
  41.  
  42. /* Output:
  43. Foo constructor
  44. Foo constructor
  45. Returned via longjmp
  46. */
  47.